package kpi.javatraining6.gnatiuk.src.tester;

import java.util.*;

import kpi.javatraining6.gnatiuk.src.teachingsystem.Difficulty;
import kpi.javatraining6.gnatiuk.src.tester.question.AnsweredQuestion;
import kpi.javatraining6.gnatiuk.src.tester.question.Question;

/**
 * Class that contains a list of questions.
 * @author Surp
 */
public class Questionnaire {

	private List<Question> questions;
	private List<Question> answeredQuestions;

    public Questionnaire(List<Question> questions) {
        this.questions = questions;
        Collections.shuffle(questions);
        answeredQuestions = new ArrayList<>();
    }

    public String getTopic() {
        if (questions.isEmpty()) {
            return "Нет вопросов";
        } else {
            return questions.get(0).getTopic();
        }
    }

	/**
	 * gets random question from the list of non answered questions and removes
	 * it from this list
	 * @return question object
	 */
	public Question nextQuestion(Difficulty difficulty){
        for (Question questionItem: questions) {
            if (questionItem.getDifficulty() == difficulty) {
                questions.remove(questionItem);
                return questionItem;
            }
        }
		throw new NoSuchElementException();
    }

	/**
	 * checks if there are questions in non answered question list
	 */
	public boolean hasMoreQuestions(Difficulty difficulty) {
         for (Question question: questions) {
              if (question.getDifficulty() == difficulty) {
                  return true;
              }
         }
		 return false;
	}

	public void putBack(Question currentQuestion){
		questions.add(0, currentQuestion);

	}
    public void putBackLastAnswered() {
        if (answeredQuestions.size() < 1) {
            return;
        }
        int index = answeredQuestions.size() - 1;// last item index
        questions.add(0, answeredQuestions.get(index));
        answeredQuestions.remove(index);
    }


	public void addToAnswered(Question question){
		answeredQuestions.add(question);
	}

    public void addToAnswered(AnsweredQuestion answeredQuestion) {
        String answered = answeredQuestion.getQuestion();
        for (Question question: questions) {
            if (question.getQuestion().equals(answered)) {
                addToAnswered(question);
            }
        }
        for (Question question: answeredQuestions) {
            questions.remove(question);
        }
    }

	/**
	 * copy of list
	 */
	private ArrayList<Question> copy(ArrayList<Question> list){
		ArrayList<Question> copy = new ArrayList<>();
		for (Question key : list) {
			copy.add(key);
		}
		return copy;
	}

	/*@Override
	public String toString() {
		int score = getTestScore();
		int maxScore=answeredQuestions.size();
		String result=String.format("your test score is %d / %d  size = %d",
                                    score, maxScore, questions.size());
		return result;
	}*/
}

